package com.JFapp.service.impl;

import com.JFapp.constants.SystemConstants;
import com.JFapp.domain.entity.Comment;
import com.JFapp.domain.entity.ResponseResult;
import com.JFapp.domain.vo.CommentVo;
import com.JFapp.domain.vo.PageVo;
import com.JFapp.eumns.AppHttpCodeEnum;
import com.JFapp.exception.SystemException;
import com.JFapp.mapper.CommentMapper;
import com.JFapp.service.CommentService;
import com.JFapp.service.UserService;
import com.JFapp.utils.BeanCopyUtils;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;
import java.util.stream.Collectors;

/**
 * (Comment)表服务实现类
 *
 * @author makejava
 * @since 2024-03-13 20:17:59
 */
@Service("commentService")
public class CommentServiceImpl extends ServiceImpl<CommentMapper, Comment> implements CommentService {

    @Autowired
    private UserService userService;

    @Override
    public ResponseResult commentList(String commentType, Long articleId, Integer pageNum, Integer pageSize) {
        //查询对应文章的跟评论
        LambdaQueryWrapper<Comment> queryWrapper=new LambdaQueryWrapper<>();
        //对articleId进行判断,只有当type为0时才判断（常量写在前面，避免空指针异常）
        queryWrapper.eq(SystemConstants.ARTICLE_COMMENT.equals(commentType),Comment::getArticleId,articleId);
        //根评论rootId为-1
        queryWrapper.eq(Comment::getRootId, SystemConstants.ROOT_COMMENT_ID);
        //判断评论类型
        queryWrapper.eq(Comment::getType,commentType);
        //分页查询
        Page<Comment> commentPage=new Page<>(pageNum,pageSize);
        page(commentPage,queryWrapper);
        //vo
        List<CommentVo> commentVoList = toCommentVoList(commentPage.getRecords());
        //查询所有根评论对应子评论的集合，并赋值；
        commentVoList.stream()
                .map(CommentVo->CommentVo.setChildren(getChildren(CommentVo.getId())))
                .collect(Collectors.toList());

        return ResponseResult.okResult(new PageVo(commentVoList, commentPage.getTotal()));
    }

    @Override
    public ResponseResult addComment(Comment comment) {
        //评论不能为空
        if (!StringUtils.hasText(comment.getContent())){
            throw new SystemException(AppHttpCodeEnum.COMMENT_NO_NULL);
        }
        save(comment);
        return ResponseResult.okResult();
    }

    /**
     *
     * @param id 根评论的id
     * @return
     */
    private List<CommentVo> getChildren(Long id){
        LambdaQueryWrapper<Comment> queryWrapper=new LambdaQueryWrapper<>();
        queryWrapper.eq(Comment::getRootId,id);
        List<Comment> commentList = list(queryWrapper);
        List<CommentVo> commentVos = toCommentVoList(commentList);
        return commentVos;
    }
    private List<CommentVo> toCommentVoList (List<Comment> list) {
        List<CommentVo> commentVos = BeanCopyUtils.copyBeanList(list, CommentVo.class);
        //遍历vo集合
        //通过creatyBy查询用户名称并赋值
        commentVos.stream()
                .map(commentVo -> commentVo.setUserName(userService.getById(commentVo.getCreateBy()).getNickName()))
                .collect(Collectors.toList());

        // 通过toCommentUserId查询toCommentUserName(昵称)
        // 如果toCommentUserId不为-1时才去查询
        for(CommentVo commentVo :commentVos){
            if(commentVo.getToCommentUserId()!=SystemConstants.ROOT_COMMENT_ID){
                String toName = userService.getById(commentVo.getToCommentUserId()).getNickName();
                commentVo.setToCommentName(toName);
            }
        }
        return commentVos;

    }
}
